package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Messages;
import ex.com.jm.food.service.AdminMessagesService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class AdminMessageAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private AdminMessagesService adminMessagesService;
	private Messages messages;
	private String messValue;
	private Integer messId;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String s_date;
	private String e_date;
	private Map<String, String> dateMap;
	public AdminMessagesService getAdminMessagesService() {
		return adminMessagesService;
	}
	public void setAdminMessagesService(AdminMessagesService adminMessagesService) {
		this.adminMessagesService = adminMessagesService;
	}
	public Messages getMessages() {
		return messages;
	}
	public void setMessages(Messages messages) {
		this.messages = messages;
	}
	public String getMessValue() {
		return messValue;
	}
	public void setMessValue(String messValue) {
		this.messValue = messValue;
	}
	public Integer getMessId() {
		return messId;
	}
	public void setMessId(int messId) {
		this.messId = messId;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(Integer initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public void setMessId(Integer messId) {
		this.messId = messId;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	/**
	 * 到达消息列表
	 * @return
	 */
	public String messList(){
		dateMap=DateUtils.getDate();
		return "messList";
	}
	
	public void getMess(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8"); 
		 int offset=(initPage-1)*pageRow;
		 List<Messages> messages=this.getAdminMessagesService().getMessages(offset, pageRow, messValue, s_date, e_date);
		 JSONArray jsonmess=JSONArray.fromObject(messages);
		 totalCount=this.getAdminMessagesService().messCount(messValue, s_date, e_date);
		 JSONObject jo=JsonUtils.buildJson(jsonmess, initPage, pageRow, totalCount);
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(jo);
			out.flush();
			out.close();
	}
	
	public String editMess(){
		if(messId!=null){
		   messages=this.getAdminMessagesService().editMess(messId);
		}
		return "edit";
	}
	
	public String saveMess(){
		if(messages.getMessValue()==null||messages.getMessValue().equals("")){
			addFieldError("messages.messValue", "消息内容不能为空");
			return "edit";
		}
		if(messages.getMessId()!=null&&!messages.equals("")){
			this.getAdminMessagesService().updateMess(messages);
			return "messList";
		}
		this.getAdminMessagesService().saveMess(messages);
		return "messList";
	}
	
	public String delMess(){
		this.getAdminMessagesService().delMess(messId);
		return "messList";
	}
}
